<template>
	<view class="message-center">
		
		<!-- 消息类型列表 -->
		<view class="message-list">
			<view 
				v-for="(item, index) in messageTypes" 
				:key="index"
				@click="goToMessageDetail(item)"
				class="message-item"
			>
				<view class="message-icon" :style="{backgroundColor: item.bgColor}">
					<image :src="item.icon" class="icon-img"></image>
				</view>
				<view class="message-content">
					<text class="message-title">{{ item.title }}</text>
				</view>
				<view class="message-right">
					<view v-if="item.num > 0" class="unread-badge">{{ item.num }}</view>
					<text class="arrow-icon">›</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { systemApi } from '@/api/index.js'

export default {
	data() {
		return {
			messageTypes: [
				{
					type: 1,
					title: '返现通知',
					icon: 'https://weixin.youpin91.com.cn/img/icon-frtz.png',
					bgColor: '#FF6B6B',
					num: 0
				},
				{
					type: 2,
					title: '物流通知',
					icon: 'https://weixin.youpin91.com.cn/img/icon-wlzt.png',
					bgColor: '#FFA726',
					num: 0
				},
				{
					type: 3,
					title: '申请通知',
					icon: 'https://weixin.youpin91.com.cn/img/icon-sqtz.png',
					bgColor: '#26C6DA',
					num: 0
				},
				{
					type: 4,
					title: '互动',
					icon: 'https://weixin.youpin91.com.cn/img/icon-hd.png',
					bgColor: '#66BB6A',
					num: 0
				},
				{
					type: 5,
					title: '系统通知',
					icon: 'https://weixin.youpin91.com.cn/img/icon-xitz.png',
					bgColor: '#42A5F5',
					num: 0
				}
			]
		}
	},
	
	onLoad() {
		this.loadNoticeData()
	},
	
	methods: {
		// 加载消息通知数据
		async loadNoticeData() {
			try {
				const res = await systemApi.getNoticeList()
				if (res.r === 0 && res.data && res.data.lists) {
					const lists = res.data.lists
					// 更新消息类型的数量
					lists.forEach((item, index) => {
						if (this.messageTypes[index]) {
							this.messageTypes[index].num = item.num
						}
					})
				}
			} catch (err) {
				console.error('获取消息数据失败:', err)
				uni.showToast({
					title: '获取消息失败',
					icon: 'none'
				})
			}
		},
		
		// 返回上一页
		goBack() {
			uni.navigateBack()
		},
		
		// 跳转到消息详情页
		goToMessageDetail(item) {
			// 跳转到通知列表页面
			uni.navigateTo({
				url: `/pages/news/notice-list?type=${item.type}&title=${item.title}`
			})
		}
	}
}
</script>

<style scoped>
.message-center {
	background-color: #f5f5f5;
	min-height: 100vh;
}

/* 头部样式 */
.header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 20rpx 30rpx;
	background: linear-gradient(135deg, #FF6B6B, #FF8E8E);
	color: white;
	height: 88rpx;
}

.header-left {
	width: 60rpx;
	height: 60rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.back-icon {
	font-size: 48rpx;
	color: white;
	font-weight: bold;
}

.header-title {
	font-size: 36rpx;
	font-weight: bold;
	color: white;
}

.header-right {
	width: 60rpx;
}

/* 消息列表样式 */
.message-list {
	padding: 20rpx 0;
}

.message-item {
	display: flex;
	align-items: center;
	padding: 30rpx;
	background-color: #fff;
	margin-bottom: 2rpx;
	position: relative;
}

.message-item:active {
	background-color: #f8f8f8;
}

.message-icon {
	width: 80rpx;
	height: 80rpx;
	border-radius: 16rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-right: 24rpx;
	flex-shrink: 0;
}

.icon-img {
	width: 48rpx;
	height: 48rpx;
}

.message-content {
	flex: 1;
	min-width: 0;
}

.message-title {
	font-size: 32rpx;
	color: #333;
	font-weight: 500;
}

.message-right {
	display: flex;
	align-items: center;
	gap: 16rpx;
}

.unread-badge {
	background-color: #FF6B6B;
	color: #fff;
	font-size: 22rpx;
	padding: 6rpx 12rpx;
	border-radius: 20rpx;
	min-width: 32rpx;
	text-align: center;
	line-height: 1;
	font-weight: bold;
}

.arrow-icon {
	font-size: 32rpx;
	color: #ccc;
	font-weight: bold;
}
</style>